package com.lc.recovery.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.lc.recovery.R;
import com.lc.recovery.http.glide.GlideApp;
import com.lc.recovery.http.response.ShopsTopBean;
import com.lc.recovery.other.IntentKey;
import com.lc.recovery.ui.activity.LoginActivity;
import com.lc.recovery.ui.activity.businesshome.ClassificationGuidelinesActivity;
import com.lc.recovery.ui.activity.recoveryhome.RecoveryShopActivity;
import com.lc.recovery.ui.activity.warehousehome.WarehousedGoodsActivity;
import com.lc.recovery.util.CourseUtils;
import com.lc.recovery.util.SPUtil;

import org.jetbrains.annotations.NotNull;

import java.io.Serializable;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class HomeAdapter extends RecyclerView.Adapter{



    public static final int TYPE_ONE = 0;
    public static final int TYPE_TWO = 1;


    private Context context;
    private String userID;
    private List<ShopsTopBean.DataBean> datas;

    public HomeAdapter(Context context, List<ShopsTopBean.DataBean> datas, String userID){
        this.context = context;
        this.datas = datas;
        this.userID = userID;
    }


    @NonNull
    @NotNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View view;
        if (viewType == TYPE_ONE){
            view = View.inflate(context, R.layout.item_home_type,null);
            ItemOneViewHolder holder = new ItemOneViewHolder(view);
            holder.titile = view.findViewById(R.id.tv_title);
            holder.img_bg=view.findViewById(R.id.img_bg);
            holder.tv_descript = view.findViewById(R.id.tv_descript);
            holder.ll_type = view.findViewById(R.id.ll_pizhi);
            return  holder;
        }else {
            view = View.inflate(context, R.layout.item_home_type_two, null);

            ItemTreeViewHolder holder = new ItemTreeViewHolder(view);
            holder.img_bg=view.findViewById(R.id.img_bg);
            holder.titile = view.findViewById(R.id.tv_title);
            holder.tv_descript = view.findViewById(R.id.tv_descript);
            holder.ll_type = view.findViewById(R.id.ll_jinshu);
            return  holder;

        }


    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull RecyclerView.ViewHolder holder, int position) {
           if (holder instanceof ItemOneViewHolder){
               ItemOneViewHolder viewHolder= (ItemOneViewHolder) holder;
               viewHolder.titile.setText(datas.get(position).getTitle());
               viewHolder.tv_descript.setText(datas.get(position).getDescript());
               GlideApp.with(context).load(datas.get(position).getPicurl2()).transform(new RoundedCorners((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, context.getResources().getDisplayMetrics())))
                       .apply(new RequestOptions().placeholder(R.mipmap.bg_01).error(R.mipmap.bg_01)).into(((ImageView) viewHolder.img_bg));
               viewHolder.ll_type.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View view) {
                       if (userID.equals("1")){
                           if (!CourseUtils.isNotNullOrEmpty(SPUtil.get(context, IntentKey.USERID, ""))) {
                               context.startActivity(new Intent(context,LoginActivity.class).putExtra("type","1"));
                               return;
                           }
                           context.startActivity(new Intent(context, RecoveryShopActivity.class).putExtra("id",datas.get(position).getId()+"").putExtra("datas", (Serializable) datas));
                       }else if (userID.equals("2")){

                           if (!CourseUtils.isNotNullOrEmpty(SPUtil.get(context, IntentKey.USERID, ""))) {
                               context.startActivity(new Intent(context,LoginActivity.class));
                               return;
                           }
                           context.startActivity(new Intent(context, ClassificationGuidelinesActivity.class).putExtra("id",datas.get(position).getId()+"").putExtra("datas", (Serializable) datas));

                       }else {
                           if (!CourseUtils.isNotNullOrEmpty(SPUtil.get(context, IntentKey.USERID, ""))) {
                               context.startActivity(new Intent(context,LoginActivity.class));
                               return;
                           }
                           context.startActivity(new Intent(context, WarehousedGoodsActivity.class).putExtra("id",datas.get(position).getId()+"").putExtra("datas", (Serializable) datas));

                       }
                   }
               });

           }else if (holder instanceof ItemTreeViewHolder){
               ItemTreeViewHolder viewHolder= (ItemTreeViewHolder) holder;
               viewHolder.titile.setText(datas.get(position).getTitle());
               viewHolder.tv_descript.setText(datas.get(position).getDescript());
               GlideApp.with(context).load(datas.get(position).getPicurl()).transform(new RoundedCorners((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                       5, context.getResources().getDisplayMetrics())))
                       .apply(new RequestOptions().placeholder(R.mipmap.bg_05).error(R.mipmap.bg_05)).into(((ImageView) viewHolder.img_bg));

               viewHolder.ll_type.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View view) {
                       if (userID.equals("1")){
                           context.startActivity(new Intent(context, RecoveryShopActivity.class).putExtra("id",datas.get(position).getId()+"").putExtra("datas", (Serializable) datas));
                       }else if (userID.equals("2")){
                           context.startActivity(new Intent(context, ClassificationGuidelinesActivity.class).putExtra("id",datas.get(position).getId()+"").putExtra("datas", (Serializable) datas));

                       }else {
                           context.startActivity(new Intent(context, WarehousedGoodsActivity.class).putExtra("id",datas.get(position).getId()+"").putExtra("datas", (Serializable) datas));

                       }
                   }
               });
           }
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    @Override
    public int getItemViewType(int position) {
        ShopsTopBean.DataBean bean = datas.get(position);
        if (bean.getType() == TYPE_ONE ){
            return TYPE_ONE;
        }else {
            return TYPE_TWO;
        }
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView)
    {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager)
        {
            GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup()
            {
                @Override
                public int getSpanSize(int position)
                {
                    //取多个item的每行占用个数的最小公倍数，
                    //比如item1一行2个，item2一行3个，item3一行1个，就取6
                    //他们对应的return 3，return 2，return 6
                    if (TYPE_ONE == getItemViewType(position))
                    {
                        return 3;
                    }  else {
                        return 2;
                    }
                }
            });
        }
    }
    class ItemOneViewHolder extends RecyclerView.ViewHolder{
        TextView titile;
        TextView tv_descript;
        LinearLayout ll_type;
        ImageView img_bg;
        public ItemOneViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }



    class ItemTreeViewHolder extends RecyclerView.ViewHolder{
        TextView titile;
        TextView tv_descript;
        ImageView img_bg;
        LinearLayout ll_type;
        public ItemTreeViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }



}
